// import 'package:app_settings/app_settings.dart';
import 'package:dailoz/dio/http_util.dart';
import 'package:dailoz/generated/l10n.dart';
import 'package:dailoz/response_json_entity.dart';
import 'package:dailoz/service/language_service/language_service.dart';
import 'package:dailoz/size_config.dart';
import 'package:dailoz/utils/showAlertDialog.dart';
import 'package:dailoz/utils/toast.dart';
import 'package:dailoz/view_model/settings_page_view_model.dart';
import 'package:dailoz/views/splash.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SettingPage extends StatefulWidget {
  const SettingPage({Key? key, this.date, this.time}) : super(key: key);

  static String id = "settingPageId";
  final DateTime? date;
  final String? time;

  @override
  State<SettingPage> createState() => _SettingPageState();
}

class _SettingPageState extends State<SettingPage> {
  // bool? notificationFlag;

  List<String> languageList = ["中文", "English"];

  int languageIndex = 0;

  String? name = "";

  void delete() {
    ShowDialog.showAlertDialog(context, "是否删除账号？", () async {
      Navigator.pop(context);
      final prefs = await SharedPreferences.getInstance();
      var response = await HttpUtil().delete("/user", {"id": prefs.getString("user-id")});
      ResponseJsonEntity responseJsonEntity = ResponseJsonEntity.fromJson(response);
      prefs.clear();
      if (!mounted) return;
      Navigator.of(context).pushNamedAndRemoveUntil(
        Splash.id,
        (Route<dynamic> route) => false,
      );
      showToast(responseJsonEntity.message);
    });
  }

  void getName() async {
    name = await LanguageService.getLanguageName();
    setState(() {});
  }

  SettingsPageViewModel? settingsPageViewModel;

  init() {
    settingsPageViewModel = SettingsPageViewModel();
    settingsPageViewModel!.fetchNotification();
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getName();
    init();
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => settingsPageViewModel,
      child: Consumer<SettingsPageViewModel>(
        builder: (context, provider, _) {
          return Scaffold(
            body: Stack(
              children: [
                Positioned(
                  top: SizeConfig.getProportionateScreenHeight(56.0),
                  left: SizeConfig.getProportionateScreenWidth(30.0),
                  child: Container(
                    height: SizeConfig.getProportionateScreenHeight(42.0),
                    width: SizeConfig.getProportionateScreenWidth(42.0),
                    decoration: const BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.all(
                        Radius.circular(14.0),
                      ),
                    ),
                    child: IconButton(
                      onPressed: () {
                        Navigator.pop(context);
                      },
                      icon: Icon(
                        Icons.arrow_back_ios_outlined,
                        size: SizeConfig.getProportionateFontSize(14.0),
                      ),
                    ),
                  ),
                ),
                Center(
                    child: SizedBox(
                  width: SizeConfig.getProportionateScreenWidth(314.0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Padding(
                        padding: const EdgeInsets.only(top: 68.0),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Text(
                              S.of(context).settings,
                              style: TextStyle(
                                color: const Color(0xff10275a),
                                fontSize: SizeConfig.getProportionateFontSize(18.0),
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                          ],
                        ),
                      ),
                      Padding(
                        padding: EdgeInsets.only(top: SizeConfig.getProportionateScreenHeight(48.0)),
                        child: Row(
                          children: [
                            Text(
                              S.of(context).general,
                              style: TextStyle(
                                color: const Color(0xff10275a),
                                fontSize: SizeConfig.getProportionateFontSize(17.0),
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                          ],
                        ),
                      ),
                      Padding(
                        padding: EdgeInsets.only(
                          top: SizeConfig.getProportionateScreenHeight(20.0),
                        ),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              S.of(context).language,
                              style: TextStyle(
                                color: const Color(0xff10275a),
                                fontSize: SizeConfig.getProportionateFontSize(16.0),
                              ),
                            ),
                            CupertinoButton(
                              child: Text(name!),
                              onPressed: () {
                                ShowDialog.showActionSheet(context, getName);
                                setState(() {});
                              },
                            ),
                          ],
                        ),
                      ),
                      provider.notificationFlag == false
                          ? Padding(
                              padding: EdgeInsets.only(
                                top: SizeConfig.getProportionateScreenHeight(20.0),
                              ),
                              child: Text(
                                S.of(context).notifications,
                                style: TextStyle(
                                  color: const Color(0xff10275a),
                                  fontSize: SizeConfig.getProportionateFontSize(17.0),
                                  fontWeight: FontWeight.bold,
                                ),
                              ),
                            )
                          : const SizedBox(),
                      provider.notificationFlag == false
                          ? Padding(
                              padding: EdgeInsets.only(
                                top: SizeConfig.getProportionateScreenHeight(20.0),
                              ),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(
                                    S.of(context).allowNotification,
                                    style: TextStyle(
                                      color: const Color(0xff10275a),
                                      fontSize: SizeConfig.getProportionateFontSize(16.0),
                                    ),
                                  ),
                                  CupertinoSwitch(
                                    dragStartBehavior: DragStartBehavior.start,
                                    activeColor: const Color(0xff5b67ca),
                                    value: provider.notificationFlag!,
                                    onChanged: (newValue) {
                                      if (provider.notificationFlag == false) {
                                        // AppSettings.openNotificationSettings();
                                        Navigator.pop(context);
                                      }
                                    },
                                  ),
                                ],
                              ),
                            )
                          : const SizedBox(),
                      Padding(
                        padding: EdgeInsets.only(
                          top: SizeConfig.getProportionateScreenHeight(20.0),
                        ),
                        child: ElevatedButton(
                          style: ElevatedButton.styleFrom(
                            backgroundColor: Colors.red,
                            shape: const RoundedRectangleBorder(
                              borderRadius: BorderRadius.all(
                                Radius.circular(12.0),
                              ),
                            ),
                            side: const BorderSide(
                              width: 1.5,
                              color: Colors.red,
                            ),
                          ),
                          onPressed: () {
                            delete();
                          },
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: <Widget>[
                              Padding(
                                padding: EdgeInsets.symmetric(
                                  vertical: SizeConfig.getProportionateScreenHeight(13.0),
                                  horizontal: SizeConfig.getProportionateScreenHeight(9.0),
                                ),
                                child: Text(
                                  S.of(context).deleteAccount,
                                  style: TextStyle(
                                    color: Colors.white,
                                    fontSize: SizeConfig.getProportionateFontSize(18.0),
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                )),
              ],
            ),
          );
        },
      ),
    );
  }
}
